<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/_aValFields.php");
require_once("./_inc/layoutFunction.php");

$errMsg = "";
$layout = sxREQUEST("layout");
$band = sxREQUEST("band");

if(sxREQ("reqtype") == "save") {
	$errMsg .= layoutBandFieldMod();
}
if(sxREQ("reqtype") == "delete") {
	$errMsg .= layoutBandFieldDel();
}
if(sxREQ("reqtype") == "") {
	$sSQL = "SELECT lf.*, la.entity FROM layout_field lf " .
			" LEFT JOIN layout_alias la ON la.layout = lf.layout AND la.layout_alias = lf.layout_alias " .
			" WHERE lf.layout = '$layout' " .
					" AND lf.band = " .sxRInt('band').
					" AND lf.row = " .sxRInt('row').
					" AND lf.seq =".sxRInt('seq');
	$objSDAO->query($sSQL, "field");
	foreach($rowField as $nX => $vX) {
		sxRDef($nX, $vX);
	}
}
sxRDef('new_row', sxReq('row'));
sxRDef('new_seq', sxReq('seq'));
sxRDef('alias_entity', sxReq('alias')."_".sxReq('entity'));
$alias_entity = sxREQ('alias_entity');
if ($alias_entity != '' and $alias_entity[0] != 'W') {
		//load attribute of alias_entity selected
		$sSQL = "SELECT a.* FROM layout_alias la" .
				" INNER JOIN attribute a ON a.entity = la.entity" .
				" WHERE la.layout_alias='".$alias_entity[0]."'" .
				" ORDER BY a.sequence";
		$objSDAO->query($sSQL, "Attr");
		if ($attribute == '') {
			$rowAtt = $objSDAO->fetch("Attr");
			$attribute = $rowAtt['attribute'];
		}	
} else {
	$attribute = ''; 
}
$rowLookup = false;
if($alias_entity != '' and $attribute != '') {
//load variable or attribute of layout_alias selected
	$sSQL = "SELECT alu.attribute " .
			" FROM attribute a " .
			" INNER JOIN domain d ON d.domain = a.domain " .
			" INNER JOIN attribute alu ON alu.entity = d.lookup_entity " .
			" WHERE a.entity = '".substr($alias_entity,2)."' AND a.attribute='".$attribute."'" .
			" ORDER BY alu.sequence";
	$objSDAO->query($sSQL, "lookup");
	$rowLookup = $objSDAO->fetch("lookup");
}

$sSQL = "SELECT lv.*, l.max_colspan, l.type FROM layout l " .
		" LEFT JOIN layout_variable lv ON l.layout = lv.layout " .
		" WHERE l.layout = '$layout'" .
		" ORDER BY lv.variable";
$objSDAO->query($sSQL, "Variab");
$rowVar = $objSDAO->fetch("Variab",0);
$max_colspan = $rowVar['max_colspan'];
$ls_type = $rowVar['type'];
//load layout_alias
$sSQL = "SELECT * FROM layout_alias WHERE layout='$layout' ORDER BY layout_alias";
$objSDAO->query($sSQL, "layout_alias");

$aOpz = array("kMenu"=>'pro.lay', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
function confDel() {
	if (confirm("Confirm ?")) {
		objFrmSubm('delete');
	}
}
</script>
<?php

echo getLOHeader($layout);

$aCmd = array(
					"del"=>array("pag"=>"javascript:confDel()",
										"title"=>_t("delete"),
										"text"=>_t("delete"),
										"perm"=>"viewmod"),
					"insMD"=>array("pag"=>"javascript:objFrmSubm('save');",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
$sCmd = (commandsHTML($aCmd));	
echo $sCmd;

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('layout');
echo getHtmlHidden('band');
echo getHtmlHidden('row');
echo getHtmlHidden('seq');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='6'>Campo</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" colspan='1'>Band*</td>" .
			"<td class=\"text01\" colspan='1'>");
echo str_pad($band,4,0,STR_PAD_LEFT);
echo("</td>" .
			"<td class=\"labl01\">Row*</td>" .
			"<td class=\"text01\" colspan='1'>");
if($ls_type=='table-line') {
	echo "0001".getHtmlHidden('new_row','1');
} else {
	echo getHtmlText('new_row',4,4);
}
echo("</td><td class=\"labl01\">Sequence*</td>" .
			"<td class=\"text01\" colspan='1'>" .
			getHtmlText('new_seq',5,5)."</td>" .
		"</tr>\n");
// label
echo("<tr>" .
			"<td class=\"titl01\" colspan='6'>Label</td>" .
		"</tr>\n");
if($ls_type!='table-line') {
	//label colspan
	echo("<tr>" .
				"<td class=\"labl01\">Number of slots</td>" .
				"<td class=\"text01\" colspan='5'><table cellpadding='0' cellspacing='0'><tr>");
	for($k=0; $k<=$max_colspan; $k++) {
		echo("<td class=\"text01\" style='text-align:center;width:30px;font-size:8pt'>$k</td>");
	}
	echo("</tr><tr>");
	for($k=0; $k<=$max_colspan; $k++) {
		$chk = (sxReq('label_colspan') == $k)?' CHECKED':'';
		echo("<td class=\"text01\" align='center'>" .
				"<input class=\"text01\" type=\"radio\" name=\"label_colspan\" value=\"$k\"$chk></td>\n");
	}
	echo("</tr></table></td>" .
			"</tr>\n");
} else {
	echo("<input type='hidden' name='label_colspan' id='label_colspan' value='0'");
}
//label
echo("<tr>" .
			"<td class=\"labl01\">Text</td>" .
			"<td class=\"text01\" colspan='5'>" .
			"<textarea class=\"inpt01\" name=\"label\" id=\"label\" cols=\"80\" rows=\"1\">".sxHE($label)."</textarea></td>" .
		"</tr>\n");		
// label properties
echo("<tr>" .
			"<td class=\"labl01\" width='20%'>Properties</td>" .
			"<td class=\"text01\" width='20%'>");
echo("<select name=\"label_attr\" id=\"label_attr\" class='inpt01'>");
foreach($aValLabelAttr as $attr){
	$sel = "";
	if($attr == sxREQ('label_attr'))
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\" width='10%'>Alignement</td>" .
			"<td class=\"text01\" width='20%'>");
echo("<select name=\"label_align\" id=\"label_align\" class='inpt01'>");
foreach($aValLabelAlign as $attr){
	$sel = "";
	if($attr == sxREQ('label_align'))
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\" width='10%'>Style</td>" .
			"<td class=\"text01\" width='20%'>");
echo("<select name=\"label_style\" id=\"label_style\" class='inpt01'>");
foreach($aValLabelStyle as $attr){
	$sel = "";
	if($attr == sxREQ('label_style'))
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select>");
echo("</td>" .
		"</tr>\n");
// value
echo("<tr>" .
			"<td class=\"titl01\" colspan='6'>Value</td>" .
		"</tr>\n");
// colspan 
echo("<tr>" .
			"<td class=\"labl01\">Number of slots</td>" .
			"<td class=\"text01\" colspan='5'><table cellpadding='0' cellspacing='0'><tr>");
for($k=0; $k<=$max_colspan; $k++) {
	echo("<td class=\"text01\" style='text-align:center;width:30px;font-size:8pt'>$k</td>");
}
echo("</tr><tr>");
for($k=0; $k<=$max_colspan; $k++) {
	$chk = (sxREQ('field_colspan') == $k)?' CHECKED':'';
	echo("<td class=\"text01\" align='center'><input class=\"text01\" type=\"radio\" name=\"field_colspan\" value=\"$k\"$chk></td>\n");
}
echo("</tr></table></td>" .
		"</tr>\n");
// alias - attribute / variable
echo("<tr>" .
			"<td class=\"labl01\">Entity</td>" .
			"<td class=\"text01\" colspan='1'>");
echo("<select name=\"alias_entity\" id=\"alias_entity\" onChange=\"objFrmSubm('reload');\" class='inpt01'>");
echo("<option value=\"\">-----</option>");
while($rowRS = $objSDAO->fetch("layout_alias")){
	if ($rowRS["layout_alias"] == 'X'){ continue; } 
	$sel = "";
	if($alias_entity == $rowRS["layout_alias"].'_'.$rowRS["entity"])
		$sel = "SELECTED";
	echo("<option value=\"".$rowRS["layout_alias"].'_'.$rowRS["entity"]."\" $sel>".
		$rowRS["layout_alias"].'_'.(($rowRS["layout_alias"]!='W')?$rowRS["entity"]:'(work-variable)')."</option>");
}
$sel = "";
if($alias_entity == "W") $sel = "SELECTED";
echo("<option value=\"W\" $sel>(work-variable)</option>");
echo("</select>");
echo("</td>\n");
if($alias_entity == ""){
	echo("<td class=\"labl01\">&nbsp;</td>" .
			"<td class=\"text01\" colspan='1'>-- spaces --</td>\n");
} elseif($alias_entity[0] == "W"){
	echo("<td class=\"labl01\">Variable</td>" .
			"<td class=\"text01\" colspan='1'>\n");
	echo("<select name=\"variable\" id=\"variable\" onChange=\"objFrmSubm('reload');\" class='inpt01'>\n");
	$pos = 0;
	while($rowVar = $objSDAO->fetch("Variab",$pos)){
		$pos = '';
		if ($rowVar["layout_alias"]=='W'){ 
			$sel = "";
			if(sxREQ('variable') == $rowVar["variable"])
				$sel = "SELECTED";
			echo("<option value=\"".$rowVar["variable"]."\" $sel>W_".$rowVar["variable"]."</option>\n");
		}
	}
	echo("</select><input type='hidden' name='attribute' value=''>\n");
} else {
	echo("<td class=\"labl01\">Attribute</td>" .
			"<td class=\"text01\" colspan='1'>\n");
	echo("<select name=\"attribute\" id=\"attribute\" onChange=\"objFrmSubm('reload');\" class='inpt01'>\n");
	$rowAtt = $objSDAO->fetch("Attr",0);
	while($rowAtt){
		$sel = "";
		if(sxREQ('attribute') == $rowAtt["attribute"])
			$sel = "SELECTED";
		echo("<option value=\"".$rowAtt["attribute"]."\" $sel>".$alias_entity[0].
			"_".$rowAtt["attribute"]."</option>\n");
		$rowAtt = $objSDAO->fetch("Attr");
	}
	echo("</select><input type='hidden' name='variable' value=''>\n");
}
echo("</td>\n");
if($ls_type!='table-line') {
	// mode- tipo
	echo("<td class=\"labl01\">Mode</td>" .
				"<td class=\"text01\" colspan='1'>");
	echo("<select name=\"field_mode\" id=\"field_mode\" class='inpt01'>");
	foreach($aValFieldMode as $val){
		$sel = "";
		if($val == sxREQ('field_mode'))
			$sel = "SELECTED";
		echo("<option value=\"$val\" $sel>$val</option>");
	}
	echo("</select></td>\n");
} else {
	echo("<td class=\"text01\" colspan='2'>&nbsp;".
			"<input type='hidden' name=\"field_mode\" id=\"field_mode\" value='display'>".
			"<input type='hidden' name=\"field_type\" id=\"field_type\" value='text'></td>\n");
}
echo("</tr>\n");	

if ($rowLookup) { // define lookup
	echo("<tr>" .
				"<td class=\"labl01\">Look-up</td>" .
				"<td class=\"text01\" colspan='5'>");
	echo("<input type='text' name='lookup_sep1' id='lookup_sep1' value=\"".
					sxHE($lookup_sep1)."\" class='inpt01' size='3' maxlength='16'>\n");
	echo("<select name=\"lookup_attr1\" id=\"lookup_attr1\" class='inpt01'>\n");
	echo("<option value=\"\">----</option>");
	$rowLookup = $objSDAO->fetch("lookup",0);
	while($rowLookup){
		$sel = "";
		if(sxREQ('lookup_attr1') == $rowLookup['attribute'])
			$sel = "SELECTED";
		echo("<option value=\"".$rowLookup['attribute']."\" $sel>".$rowLookup['attribute']."</option>");
		$rowLookup = $objSDAO->fetch("lookup");
	}
	echo("</select>\n");
	echo("<input type='text' name='lookup_sep2' id='lookup_sep2' value=\"".
					sxHE($lookup_sep2)."\" class='inpt01' size='3' maxlength='16'>\n");
	echo("<select name=\"lookup_attr2\" id=\"lookup_attr2\" class='inpt01'>\n");
	echo("<option value=\"\">----</option>");
	$rowLookup = $objSDAO->fetch("lookup",0);
	while($rowLookup){
		$sel = "";
		if(sxREQ('lookup_attr2') == $rowLookup['attribute'])
			$sel = "SELECTED";
		echo("<option value=\"".$rowLookup['attribute']."\" $sel>".$rowLookup['attribute']."</option>");
		$rowLookup = $objSDAO->fetch("lookup");
	}
	echo("</select>\n");
	echo("<input type='text' name='lookup_sep3' id='lookup_sep3' value=\"".
					sxHE($lookup_sep3)."\" class='inpt01' size='3' maxlength='16'>\n");
	echo("<select name=\"lookup_attr3\" id=\"lookup_attr3\" class='inpt01'>\n");
	echo("<option value=\"\">----</option>");
	$rowLookup = $objSDAO->fetch("lookup",0);
	while($rowLookup){
		$sel = "";
		if(sxREQ('lookup_attr3') == $rowLookup['attribute'])
			$sel = "SELECTED";
		echo("<option value=\"".$rowLookup['attribute']."\" $sel>".$rowLookup['attribute']."</option>");
		$rowLookup = $objSDAO->fetch("lookup");
	}
	echo("</select>\n");
	echo("<input type='text' name='lookup_sep4' id='lookup_sep4' value=\"".
					sxRHE('lookup_sep4')."\" class='inpt01' size='3' maxlength='16'>\n");
	echo("</td>\n</tr>\n");	
}

// field properties
echo("<tr>" .
			"<td class=\"labl01\">Display field properties</td>" .
			"<td class=\"text01\">");
echo("<select name=\"field_attr\" id=\"field_attr\" class='inpt01'>");
foreach($aValFieldAttr as $attr){
	$sel = "";
	if($attr == sxREQ('field_attr'))
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\">Alignement</td>" .
			"<td class=\"text01\">");
echo("<select name=\"field_align\" id=\"field_align\">");
foreach($aValFieldAlign as $attr){
	$sel = "";
	if($attr == sxREQ('field_align'))
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\">Style</td>" .
			"<td class=\"text01\">");
echo("<select name=\"field_style\" id=\"field_style\" class='inpt01'>");
foreach($aValFieldStyle as $attr){
	$sel = "";
	if($attr == sxREQ('field_style'))
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select>");
echo("</td>" .
		"</tr>\n");
// field format
echo("<tr>" .
			"<td class=\"labl01\">Dispaly field format</td>" .
			"<td class=\"text01\">");
echo("<select name=\"field_format\" id=\"field_format\" class='inpt01'>");
echo("<option value=\"\">----</option>");
foreach($aValFieldFormat as $val){
	$sel = "";
	if($val == sxREQ('field_format'))
		$sel = "SELECTED";
	echo("<option value=\"$val\" $sel>$val</option>");
}
echo("</select></td>\n");
echo("<td class=\"labl01\">Parameters</td>" .
			"<td class=\"text01\" colspan='3'><input class=\"inpt01\" type=\"text\" name=\"field_format_par\" id=\"field_format_par\" size=\"64\" maxlength='128' value=\"$field_format_par\"></td>" .
		"</tr>\n");
// input type
echo("<tr>\n" .
			"<td class=\"labl01\">Input type</td>" .
			"<td class=\"text01\" colspan='1'>");
echo("<select name=\"field_type\" id=\"field_type\" class='inpt01'>");
foreach($aValFieldType as $attr){
	$sel = "";
	if($attr == sxREQ('field_type'))
		$sel = "SELECTED";
	echo("<option value=\"$attr\" $sel>$attr</option>");
}
echo("</select>");
echo("</td>" .
			"<td class=\"labl01\">Width</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"field_width\" " .
			"id=\"field_width\" size=\"5\" value=\"".sxRHE('field_width')."\"></td>\n");
echo("<td class=\"labl01\">Height</td>" .
			"<td class=\"text01\" colspan='1'><input class=\"inpt01\" type=\"text\" name=\"field_height\" id=\"field_height\" size=\"5\" value=\"$field_height\"></td>");
echo("</tr>\n");
// value array onChange
echo("<tr>\n" .
			"<td class=\"labl01\">Value array</td>" .
			"<td class=\"text01\" colspan='2'>");
echo("<select name=\"values_array\" id=\"values_array\" class='inpt01'>\n");
echo("<option value=\"\" >---</option>\n");
$pos = 0;
while($rowVar = $objSDAO->fetch("Variab",$pos)) {
	$pos = '';
	if ($rowVar["layout_alias"] != 'X') {continue;}
	$sel = "";
	if(sxREQ('values_array') == $rowVar["variable"])
		$sel = "SELECTED";
	echo("<option value=\"".$rowVar["variable"]."\" $sel>X_".$rowVar["variable"]."</option>\n");
}
echo("</select></td>\n");
echo("</td>" .
			"<td class=\"labl01\" colspan='2' align='right'>Reload if value changed : </td>" .
			"<td class=\"text01\">" .
			"<input class=\"text01\" type=\"radio\" name=\"on_change_reload\" id=\"on_change_reload\"".
			" value='Y' ".((sxREQ('on_change_reload')=='Y')?'CHECKED':'')."> yes &nbsp;&nbsp;&nbsp;" .
			"<input class=\"text01\" type=\"radio\" name=\"on_change_reload\" id=\"on_change_reload\"".
			" value='N' ".((sxREQ('on_change_reload')=='N')?'CHECKED':'')."> No &nbsp;&nbsp;&nbsp;" .
			"</td>\n");
echo("</tr>\n");
// check variable
echo "<tr>";
echo("<td class=\"labl01\" colspan='1'>Enabling values</td>" .
		"<td class=\"text01\" colspan='2'>\n");
echo("<select name=\"check_variable\" id=\"check_variable\" class='inpt01'>\n");
echo("<option value=\"\" >---</option>\n");
$pos = 0;
while($rowVar = $objSDAO->fetch("Variab",$pos)) {
	$pos = '';
	if ($rowVar["layout_alias"] != 'W') {continue;}
	$sel = "";
	if(sxREQ('check_variable') == $rowVar["variable"])
		$sel = "SELECTED";
	echo("<option value=\"".$rowVar["variable"]."\" $sel>W_".$rowVar["variable"]."</option>\n");
}
echo "</select></td>\n";
echo "<td class=\"labl01\" colspan='2' align='right'>Default validation : </td>" .
			"<td class=\"text01\">";
echo("<select name=\"field_check\" id=\"field_check\" class='inpt01'>");
echo("<option value=\"\">----</option>");
foreach($aValFieldCheck as $val){
	$sel = "";
	if($val == sxREQ('field_check'))
		$sel = "SELECTED";
	echo("<option value=\"$val\" $sel>$val</option>");
}
echo("</select></td>\n");
echo "</tr>\n";
// upgrade status
$aVal = array('S'=>'Strutturale',
	'M'=>'Modificabile non cancellabile',
	'C'=>'Modificabile e cancellabile',
	'D'=>'Cancellato',
	'U'=>'Modificato');
echo("<tr>" .
			"<td class=\"labl01\">Upgrade status</td>" .
			"<td class=\"text01\" colspan='5'>".getHTMLRadio('upgrade_status', $upgrade_status, $aVal)."</td>" .
		"</tr>\n");
// value list
echo("<tr>" .
			"<td class=\"labl01\">List of values</td>" .
			"<td class=\"text01\" colspan='5'>".getHtmlTextArea('field_value_list',100,5)."</td>" .
		"</tr>\n");		
// predefined HTML 
echo("<tr>" .
			"<td class=\"titl01\" colspan='6'>Predefined HTML </td>" .
		"</tr>\n");
// string HTML 
echo("<tr>" .
			"<td class=\"labl01\">HTML string</td>" .
			"<td class=\"text01\" colspan='5'>".getHtmlTextArea('html_string',100,5)."</td>" .
		"</tr>\n");			
echo("</table>");
echo $sCmd;
echo("</form>");
echo(footerHTML());
exit();

function layoutBandFieldDel(){
 	global $objSDAO;
	$errMsg = "";

	$sSQL = "DELETE FROM layout_field " .
				"WHERE layout='".sxREQ('layout')."'  AND band=".sxRInt('band')
				." AND row=".sxRInt('row')." AND seq=".sxRInt('seq')."";	
	$objSDAO->query($sSQL,"del");
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'abnormal end';
 } 
 
function layoutBandFieldMod() {
 	global $objSDAO;
	$errMsg = "";
	if(sxREQ('new_row') == "")
		$errMsg .= "missing row<br>";
	if(preg_match("/[^0-9]/",sxREQ('new_row')))
		$errMsg .= "not numeric row<br>";
	if(sxREQ('new_seq') == "")
		$errMsg .= "missing seq.<br>";
	if(preg_match("/[^0-9]/",sxREQ('new_seq')))
		$errMsg .= "not numeric seq.<br>";
	if(preg_match("/[^0-9]/",sxREQ('label_colspan')))
		$errMsg .= "not numeric label_colspan<br>";
	if(preg_match("/[^0-9]/",sxREQ('label_colspan')))
		$errMsg .= "not numeric label_colspan<br>";
	if(preg_match("/[^0-9]/",sxREQ('field_colspan')))
		$errMsg .= "not numeric field_colspan<br>";
	if(preg_match("/[^0-9]/",sxREQ('field_width')))
		$errMsg .= "not numeric field_width<br>";
	if(preg_match("/[^0-9]/",sxREQ('field_height')))
		$errMsg .= "not numeric field_height<br>";
	if (sxREQUEST('field_colspan')+sxREQUEST('label_colspan') <= 0) {
		$errMsg .= _t("numero colonne campo deve essere > 0").".<br>";
	}
	$field = sxRAS('layout')."/".intval(sxRAS('band'))."/".intval(sxRAS('row'))."/".intval(sxRAS('seq'));
		
	//check space
	$sSQL = "SELECT lf.*, l.max_colspan FROM layout l " .
				" LEFT JOIN layout_field lf ON lf.layout=l.layout " .
						" AND lf.band = '".sxRAS('band')."'".
						" AND lf.row = '".sxRAS('new_row')."' AND lf.seq <> '".sxRAS('seq')."'".
				" WHERE l.layout = '".sxRAS('layout')."'";
	//dbg_($sSQL);
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	$numCelleRes = $row["max_colspan"];
	$label_colspan = intval(sxREQUEST("label_colspan"));
	$field_colspan = intval(sxREQUEST("field_colspan"));
	
	while($row){
		$numCelleRes = $numCelleRes - $row["label_colspan"]  - $row["field_colspan"]; 
		$row = $objSDAO->fetch("test");
	}
	if($numCelleRes - $label_colspan - $field_colspan < 0){
		$errMsg .= _t("not enough space (max %s cols.)",$numCelleRes)."<br>";
	}
	//existence
	if (sxREQUEST('row')!= sxREQUEST('new_row') or sxREQUEST('seq')!= sxREQUEST('new_seq')) {
		$sSQL = "SELECT count(*) AS num FROM layout_field " .
				"WHERE layout=".intval(sxRAS('layout'))." " .
						"AND band=".intval(sxRAS('band'))." " .
						"AND row=".intval(sxRAS('new_row'))." AND seq = ".intval(sxRAS('new_seq'));
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if ($row['num'] != 0) {
			$errMsg .= _t("existing row/sequence")."<br>";
		}
 	}

	if($errMsg != "") {
		return $errMsg; 	
 	}		
	$sSQL = "UPDATE layout_field SET " .
			"layout_alias='".substr(sxRAS('alias_entity'),0,1)."', " .
			"entity='".substr(sxRAS('alias_entity'),2)."', " .
			"attribute='".sxRAS('attribute')."', " .
			"variable='".sxRAS('variable')."', " .
			"label_colspan=".intval(sxRAS('label_colspan')).", " .
			"label='".sxRAS('label')."', " .
			"label_attr='".sxRAS('label_attr')."', " .
			"label_align='".sxRAS('label_align')."', " .
			"label_style='".sxRAS('label_style')."', " .
			"field_mode='".sxRAS('field_mode')."', " .
			"field_colspan=".intval(sxRAS('field_colspan')).", " .
			"field_type='".sxRAS('field_type')."', " .
			"field_value_list='".sxRAS('field_value_list')."', " .
			"field_attr='".sxRAS('field_attr')."', " .
			"field_align='".sxRAS('field_align')."', " .
			"field_style='".sxRAS('field_style')."', " .
			"field_format='".sxRAS('field_format')."', " .
			"field_format_par='".sxRAS('field_format_par')."', " .
			"field_width=".intval(sxRAS('field_width')).", " .
			"field_height=".intval(sxRAS('field_height')).", " .
			"field_check='".sxRAS('field_check')."', " .
			"check_variable='".sxRAS('check_variable')."', " .
			"lookup_sep1='".sxRAS('lookup_sep1')."', " .
			"lookup_sep2='".sxRAS('lookup_sep2')."', " .
			"lookup_sep3='".sxRAS('lookup_sep3')."', " .
			"lookup_sep4='".sxRAS('lookup_sep4')."', " .
			"lookup_attr1='".sxRAS('lookup_attr1')."', " .
			"lookup_attr2='".sxRAS('lookup_attr2')."', " .
			"lookup_attr3='".sxRAS('lookup_attr3')."', " .
			"upgrade_status='".sxRAS('upgrade_status')."', " .
			"values_array='".sxRAS('values_array')."', " .
			"on_change_reload='".sxRAS('on_change_reload')."', " .
			"html_string='".sxRAS('html_string')."' " .				
			"WHERE AND layout = ".sxRAS('layout')." AND band = ".sxRint('band')." " .
					" AND row = ".sxRint('row')." AND seq = ".sxRint('seq')."";	
	$objSDAO->query($sSQL,"upd");
	if (sxREQUEST('row')!= sxREQUEST('new_row') or sxREQUEST('seq')!= sxREQUEST('new_seq')) {
		$sSQL = "UPDATE layout_field SET row = ".sxRAS('new_row').", seq = ".sxRAS('new_seq').
			" WHERE layout = '".sxRAS('layout')."' AND band = ".sxRint('band').
					" AND row = ".sxRint('row')." AND seq = ".sxRint('seq');	
		$objSDAO->exec($sSQL,"test");
 	}
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'abnormal end';
 }
 